package com.linkage.educloud.ah.adapter.topic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.widget.ExpressionUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Mr'Tang on 2018/12/5.
 */

public class TopicChildDetailCommentAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<TopicChildBean> mData;

    private OnitemClickListener onitemClickListener;

    public TopicChildDetailCommentAdapter(Context context, List<TopicChildBean> apps) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mData = apps;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public TopicChildBean getItem(int position) {
        return mData.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_topic_detail_comment, null);
            viewHolder = new ViewHolder();
            viewHolder.item_topic_detail_comment_avater = convertView.findViewById(R.id.item_topic_detail_comment_avater);
            viewHolder.item_topic_detail_comment_nick = convertView.findViewById(R.id.item_topic_detail_comment_nick);
            viewHolder.item_topic_detail_comment_time = convertView.findViewById(R.id.item_topic_detail_comment_time);
            viewHolder.item_topic_detail_comment_content = convertView.findViewById(R.id.item_topic_detail_comment_content);
            viewHolder.item_topic_detail_comment_priase_lin = convertView.findViewById(R.id.item_topic_detail_comment_priase_lin);
            viewHolder.item_topic_detail_comment_priase_text = convertView.findViewById(R.id.item_topic_detail_comment_priase_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final TopicChildBean app = getItem(position);
        ImageLoader.loadAds(mContext, app.getFace(), viewHolder.item_topic_detail_comment_avater);
        viewHolder.item_topic_detail_comment_nick.setText(app.getNick());
        viewHolder.item_topic_detail_comment_content.setText(ExpressionUtils.replaceFace(mContext, app.getContent()));
        viewHolder.item_topic_detail_comment_time.setText(app.getPublish_time());
        viewHolder.item_topic_detail_comment_priase_text.setText(String.valueOf(app.getZan_num()));
        viewHolder.item_topic_detail_comment_priase_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClickListener.onItemClick(position);
            }
        });
        return convertView;
    }

    class ViewHolder {
        private CircleImageView item_topic_detail_comment_avater;
        private TextView item_topic_detail_comment_nick;
        private TextView item_topic_detail_comment_time;
        private TextView item_topic_detail_comment_content;
        private LinearLayout item_topic_detail_comment_priase_lin;
        private TextView item_topic_detail_comment_priase_text;
    }

    //点赞回调接口
    public interface OnitemClickListener {
        void onItemClick(int position);
    }

    public void setOnitemClickListener(OnitemClickListener onItemActionClick) {
        this.onitemClickListener = onItemActionClick;
    }
}
